package com.hakim.wfb.admin.filter;

import com.hakim.wfb.admin.enums.ErrorCodeEnum;
import com.hakim.wfb.bean.Menu;
import com.hakim.wfb.dao.MenuDao;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.validate.ValidateFilter;
import com.hkfs.fundamental.validate.annotaion.ValidateFilterBean;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@ValidateFilterBean
public class AddMenuFilter extends ValidateFilter<AddMenuFilter.AddMenuFilterParam> {

    @Autowired
    private MenuDao menuDao;

    @Override
    protected void prcoess(AddMenuFilterParam param, ResultEx resultEx){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentId", param.getParentId());
        map.put("menuName", param.getMenuName());
        int count = menuDao.siblingUnique(map);
        if(count > 0){
            resultEx.makeResult(ErrorCodeEnum.MENU_NAME_EXISTS.getCode(), ErrorCodeEnum.MENU_NAME_EXISTS.getMessage());
            return;
        }
        if(param.getParentId() > 0){
            Menu parentMenu = menuDao.getById(param.getParentId());
            if(parentMenu == null){
                resultEx.makeResult(ErrorCodeEnum.MENU_PARENT_NOT_EXISTS.getCode(), ErrorCodeEnum.MENU_PARENT_NOT_EXISTS.getMessage());
                return;
            }
        }
        resultEx.makeSuccessResult();
    }
    public static class AddMenuFilterParam{
        @NotNull(message = "父菜单ID不能为空")
        private Long parentId;
        @NotEmpty(message = "菜单名称不能为空")
        private String menuName;

        public String getMenuName() {
            return menuName;
        }

        public void setMenuName(String menuName) {
            this.menuName = menuName;
        }

        public Long getParentId() {
            return parentId;
        }

        public void setParentId(Long parentId) {
            this.parentId = parentId;
        }
    }
}
